package com.ssf.hsdtouch.fragment;

import com.ssf.hsdtouch.MainActivity;
import com.ssf.hsdtouch.R;
import com.ssf.hsdtouch.R.id;
import com.ssf.hsdtouch.R.layout;
import com.ssf.hsdtouch.entiy.FileObj;
import com.ssf.hsdtouch.util.Constants;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore.Images;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoViewFragment extends Fragment {
	private Context context;
	private VideoView wbVideo;
	private ImageView play, playbg;
	private View view;
	private String urlPath = "";
	private boolean isPlaying = false;

	@Override
	public void onDestroy() {
		Log.v("video", "onDestroy");
		if (receiver != null) {
			context.unregisterReceiver(receiver);
		}
		super.onDestroy();
	}

	@Override
	public void onPause() {
		stopPlaying();
		super.onPause();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.layout_video, null);
		Log.v("test", "onCreateView");
		if (getArguments() != null) {
			FileObj obj = (FileObj) getArguments().getSerializable("file");
			if (obj != null) {
				String path = obj.getFilePath();
				Log.v("test", "path onCreateView = " + path);
			}
		}
		initView();
		return view;
	}

	private void initView() {
		wbVideo = (VideoView) view.findViewById(R.id.video);
		play = (ImageView) view.findViewById(R.id.play);
		Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(urlPath,
				Images.Thumbnails.FULL_SCREEN_KIND);
//		int w = 0;
//		int h = 0;
//		Log.v("test", "bitmap w = " + bitmap.getWidth());
//		Log.v("test", "bitmap h = " + bitmap.getHeight());
//		float ratio = Float.valueOf(bitmap.getWidth() + "")
//				/ Float.valueOf(bitmap.getHeight() + "");
//		Log.v("test", "ratio = " + ratio);
//
//		if (ratio > 1.0f) {
//			w = 1328;
//			h = (int) ((int) 1328 / ratio);
//		} else {
//			h = 1080;
//			w = (int) ((int) 1080 * ratio);
//		}
//
//		wbVideo.setLayoutParams(new FrameLayout.LayoutParams(w, h));//
//		
//		Log.v("test", "h = " + h);
//		Log.v("test", "w = " + w);

		// Log.v("test", " wbVideo h = " + wbVideo.getMeasuredHeight());
		// Log.v("test", " wbVideo w = " + wbVideo.getMeasuredWidth());
		//
		// Log.v("test", " wbVideo1 h = " + wbVideo.getHeight());
		// Log.v("test", " wbVideo1 w = " + wbVideo.getWidth());

		playbg = (ImageView) view.findViewById(R.id.playbg);
		playbg.setImageBitmap(bitmap);
		play.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				playVideo();
			}
		});
		wbVideo.setMediaController(null);
		wbVideo.setOnErrorListener(new MediaPlayer.OnErrorListener() {

			@Override
			public boolean onError(MediaPlayer mp, int what, int extra) {
				if (what == MediaPlayer.MEDIA_ERROR_UNKNOWN) {
					Toast.makeText(context, "MEDIA_ERROR_UNKNOWN",
							Toast.LENGTH_SHORT).show();
				} else if (what == MediaPlayer.MEDIA_ERROR_SERVER_DIED) {
					Toast.makeText(context, "MEDIA_ERROR_SERVER_DIED",
							Toast.LENGTH_SHORT).show();
				} else if (what == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
					Toast.makeText(context,
							"MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK",
							Toast.LENGTH_SHORT).show();
				}
				return false;
			}
		});

		wbVideo.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			@Override
			public void onCompletion(MediaPlayer mp) {
				Log.v("test", "BC_SHOW_NEXT");
				context.sendBroadcast(new Intent(Constants.BC_SHOW_NEXT));
				resumeTouchThread();
				if (((MainActivity) context).CurStatus == ((MainActivity) context).STATUS_OP) {
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							play.setVisibility(View.VISIBLE);
							playbg.setVisibility(View.VISIBLE);
						}
					}, 400);
				}
			}
		});
	}

	private void resumeTouchThread() {
		if (((MainActivity) getActivity()) != null) {
			if (((MainActivity) getActivity()).CurStatus == ((MainActivity) getActivity()).STATUS_OP) {
				((MainActivity) getActivity()).touchFlag = true;
			}
		}
	}

	public void stopPlaying() {
		resumeTouchThread();
		if (wbVideo != null) {
			wbVideo.stopPlayback();
			wbVideo.setVisibility(View.GONE);
		}
		if (play != null) {
			play.setVisibility(View.VISIBLE);
		}
		if (playbg != null) {
			playbg.setVisibility(View.VISIBLE);
		}
		isPlaying = false;
	}

	StopReceiver receiver;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = getActivity();
		Log.v("test", "video onCreate ");
		FileObj obj = (FileObj) getArguments().getSerializable("file");
		urlPath = obj.getFilePath();
		Log.v("test", "urlPath onCreate = " + urlPath);
		receiver = new StopReceiver();
		context.registerReceiver(receiver, new IntentFilter(
				Constants.BC_STOP_PLAYING));
	}

	public void playVideo() {
		if (((MainActivity) context) != null) {
			((MainActivity) context).touchFlag = false;
		}
		Log.v("test", "urlPath = " + urlPath);
		if (TextUtils.isEmpty(urlPath)) {
			return;
		}
		wbVideo.setVideoPath(urlPath);
		wbVideo.requestFocus();
		wbVideo.requestFocusFromTouch();
		wbVideo.start();
		play.setVisibility(View.GONE);
		playbg.setVisibility(View.GONE);
		wbVideo.setVisibility(View.VISIBLE);
		isPlaying = true;
		Log.v("test", "playVideo end");
	}

	class StopReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			stopPlaying();
		}
	}

}